﻿using AiShow.Commen.Vod;
using AiShow.IBLL;
using AiShow.IDAL;
using AiShow.Model;
using AiShow.Model.OperResult;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiShow.BLL
{

         /// 视频业务管理类
         /// </summary>
        public class VideoManager : IVideoManager
        {
            /// <summary>
            /// 待上传视频
            /// </summary>
            /// <param name="userId">用户Id</param>
            /// <param name="fileName">文件名</param>
            /// <param name="headline">标题</param>
            /// <param name="location">位置</param>
            /// <returns></returns>
            public OperResult<CreateUploadVideoResult> ToBeUpload(int userId, string fileName, string headline, string location = null)
            {
                // 请求接口
                IVodService vodService = new VodService();
                CreateUploadVideoResult createResult = vodService.CreateUploadVideo(headline, fileName);
                // 写入数据库
                IVideoService videoService = new VideoService();
                TBVideos video = videoService.ToBeUpload(createResult.VideoId, userId, headline, location);
                // 返回结果
                return OperResult<CreateUploadVideoResult>.Succeed(createResult);
            }

            /// <summary>
            /// 同步视频
            /// </summary>
            /// <returns></returns>
            public async Task<OperResult<int>> SyncVideos()
            {
                IVideoService videoService = new VideoService();
                List<string> processVideoIds = videoService.GetInProcessVideoIds();
                IVodService vodService = new VodService();
                GetVideoInfosResult getResult = vodService.GetVideoInfos(processVideoIds);
                List<TBVideos> videos = getResult.VideoList.Select(m => new TBVideos()
                {
                    VideoId = m.VideoId,
                    CoverURL = m.CoverURL,
                    Status = (int)m.Status
                }).ToList();
                await videoService.UpdateVideos(videos);
                OperResult<int> result = OperResult<int>.Succeed(videos.Count);
                return await Task.FromResult(result);
            }

            /// <summary>
            /// 初始化播放
            /// </summary>
            /// <param name="videoId">视频Id</param>
            /// <returns></returns>
            public OperResult<GetPlayInfoResult> InitPlay(string videoId)
            {
                IVodService vodService = new VodService();
                GetPlayInfoResult getResult = vodService.GetPlayInfo(videoId);
                return OperResult<GetPlayInfoResult>.Succeed(getResult);
            }

            /// <summary>
            /// 获取视频列表
            /// </summary>
            /// <returns></returns>
            public OperResult<List<TBVideos>> GetVideos()
            {
                IVideoService videoService = new VideoService();
                List<TBVideos> videos = videoService.GetVideos();
                return OperResult<List<TBVideos>>.Succeed(videos);
            }

            /// <summary>
            /// 通过视频Id获取视频信息
            /// </summary>
            /// <param name="videoId"></param>
            /// <returns></returns>
            public OperResult<TBVideos> GetVideoByVideoId(string videoId)
            {
                IVideoService videoService = new VideoService();
                TBVideos video = videoService.GetVideoByVideoId(videoId);
                return OperResult<TBVideos>.Succeed(video);
            }

            /// <summary>
            /// 通过用户Id获取视频列表
            /// </summary>
            /// <param name="userId"></param>
            /// <returns></returns>
            public OperResult<List<TBVideos>> GetVideosByUserId(int userId)
            {
                IVideoService videoService = new VideoService();
                List<TBVideos> videos = videoService.GetVideosByUserId(userId);
                return OperResult<List<TBVideos>>.Succeed(videos);
            }

            /// <summary>
            /// 删除完整视频
            /// </summary>
            /// <param name="videoIds">视频Id</param>
            public void DeleteVideo(string videoId)
            {
                // 从数据库删除
                IVideoService videoService = new VideoService();
                videoService.DeleteVideo(videoId);
                // 从阿里云删除
                IVodService vodService = new VodService();
                vodService.DeleteVideos(new List<string>() { videoId });
            }
        }
}